package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.mapper.EmployeeMapper;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }


//    @PostMapping()
//    @ApiOperation(value = "新增员工")
//    public Result save(@RequestBody EmployeeDTO employeeDTO) {
//        log.info("新增员工：{}", employeeDTO);
//
//        employeeService.sava(employeeDTO);
//        return Result.success();
//
//    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 新增信息
     * @param employeeDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工，员工信息：{}",employeeDTO);
        employeeService.saveByMybatis(employeeDTO);


    	return Result.success();
    }

    /**
     * 分页查询. get查询是和前端保持一致
     * @param pageQueryDTO
     * @return
     */

    @GetMapping("/page")
    public Result pageQuery(@RequestBody EmployeePageQueryDTO pageQueryDTO){
        // select * from employee where name like %?%  limit ?,?
        // 利用 pageHelper.
//        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
//
//        // 利用这个插件，得到的结果就是固定的， 就是 page<xxx>
//        Page<Employee> page = employeeService.pageQuery(pageQueryDTO);
//        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());

        // 利用 MybatisPlus 来操作
        Page<Employee> page = new Page<>(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.like(pageQueryDTO.getName()!= null, "username", pageQueryDTO.getName());
        Page<Employee> employeePage = employeeMapper.selectPage(page, wrapper);

        PageResult pageResult = new PageResult(employeePage.getTotal(), employeePage.getRecords());
        return Result.success(pageResult);
    }


    /**
     * 不是查询返回结果的话可以不用泛型
     * 启用禁用员工账号
     * @param status
     * @param id
     * @return
     */
//    @PostMapping("/status/{status}")
//    public Result startOrStop(@PathVariable Integer status, Long id) {
//        log.info("启用或者禁止员工帐号，原状态：{}，账号id：{}", status, id );
//        employeeService.startOrStop(status, id);
//        return Result.success();
//    }

    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result StartOrStop(@PathVariable Integer status, @RequestParam("id") Long id) {
        log.info("启用或者禁止员工账号，状态：{}，账号id：{}", status, id);
        Employee employee = new Employee();
        employee.setId(id);
        employee.setStatus(status);
        employeeMapper.updateByMybatis(employee);
        return Result.success();


    }

    /**
     * 根据Id查询员工信息
     */
    @PostMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> getById(@PathVariable("id") Long id){
        // 单个参数用 @ResponseBody 好像不可以
        Employee employee = employeeMapper.selectById(id);
        log.info("根据id查询员工信息：{}", employee);
        return Result.success(employee);

    }

    /**
     * 编辑员工信息
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息:{}",employeeDTO);
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);

        employeeMapper.updateById(employee);
        return Result.success();
    }


}
